/*
*   This file is part of NSMB Editor 5.
*
*   NSMB Editor 5 is free software: you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation, either version 3 of the License, or
*   (at your option) any later version.
*
*   NSMB Editor 5 is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with NSMB Editor 5.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using NSMBe4.NSBMD;
using NSMBe4;
using System.IO;

namespace MKDS_Course_Editor.NSBMD
{
    public class NSBTP
    {
        string f;
        byte[] data;
        uint patDataOffset;
        public int texcount = 0;
        public int palcount = 0;
        public int framecount = 0;
        int texOffset;
        int palOffset;
        int keyframes;
        public string[] texname = new string[90];
        public string[] palname = new string[90];
        

        public Texture[] textures;
        public NSMBe4.NSBMD.Palette[] palettes;
        public ByteArrayInputStream str;

        public NSBTP(string f)
        {

            this.f = f;
            data = System.IO.File.ReadAllBytes(f);
            str = new ByteArrayInputStream(data);
            if (str.readUInt() == 0x37375A4C) //LZ77
            {
                byte[] ndata = new byte[data.Length - 4];
                Array.Copy(data, 4, ndata, 0, ndata.Length);

                data = ROM.LZ77_Decompress(ndata);
                str = new ByteArrayInputStream(data);
            }

            //look for TEX0 block
            //ugly, but i'm lazy to implement it properly.
            bool found = false;
            while (str.lengthAvailable(4))
            {
                uint v = str.readUInt();
                if (v == 0x30544150) // "TEX0"
                {
                    str.setOrigin(str.getPos() - 4);
                    found = true;
                    break;
                }
                else
                    str.skipback(3); //just in case its not word-aligned
            }
            str.seek(0);
            if (!found)
            {
                textures = new Texture[0];
                palettes = new NSMBe4.NSBMD.Palette[0];
                return;
            }
            byte[] tmp = { 0, 0 };
            //Read stuff
            str.seek(0x34);
            tmp[0] = str.readByte();
            tmp[1] = str.readByte();
            framecount = parselittle(System.BitConverter.ToString(tmp).Replace("-", "") + "0000");

            texcount = Convert.ToInt32(str.readByte().ToString());
            palcount = Convert.ToInt32(str.readByte().ToString());

            tmp[0] = str.readByte();
            tmp[1] = str.readByte();
            texOffset = parselittle(System.BitConverter.ToString(tmp).Replace("-", "") + "0000");
            tmp[0] = str.readByte();
            tmp[1] = str.readByte();
            palOffset = parselittle(System.BitConverter.ToString(tmp).Replace("-", "") + "0000");

            str.seek(texOffset+48);
            for (int i = 0; i < texcount; i++)
            {
                string tmp2 = "";
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                texname[i] = tmp2.Replace('\0'.ToString(), "");
            }
            for (int i = 0; i < palcount; i++)
            {
                string tmp2 = "";
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                tmp2 += Convert.ToChar(Convert.ToInt32(str.readByte())).ToString();
                palname[i] = tmp2.Replace('\0'.ToString(), "");
            }
        }
        int parselittle(string input)
        {
            int test = 0;
            test = int.Parse(input.ToCharArray()[6].ToString() + input.ToCharArray()[7].ToString() + input.ToCharArray()[4].ToString() + input.ToCharArray()[5].ToString() + input.ToCharArray()[2].ToString() + input.ToCharArray()[3].ToString() + input.ToCharArray()[0].ToString() + input.ToCharArray()[1].ToString(), System.Globalization.NumberStyles.HexNumber);
            return test;
        }
    }
}
